﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MovieReview.DataModels;
using MovieReview.Data;

namespace MovieReview.Web.Controllers
{
    public class HomeController : Controller
    {
        /***********READ************/
        [HttpGet]
        public ActionResult Index()
        {
            List<Movie> Movies = new List<Movie>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Movies = db.Movies.ToList();
            }
            return View(Movies);
        }
        /***************CREATE***************/
        public ActionResult AddMovie()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddMovie(string title, string image, int rating)
        {/*Create*/
            Movie m = new Movie()
            {
                Title = title,
                Image = image,
                Rating = rating
            };
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Movies.Add(m);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult Details(int Id)
        {
            ViewBag.Message = "Your application description page.";
            Movie target = new Movie();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                target = db.Movies.Where(x => x.Id == Id).FirstOrDefault();

            }
            return View(target);

        }
        public ActionResult Edit(int id)
        {
            Movie m = new Movie();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                m = db.Movies.Where(x => x.Id == id).FirstOrDefault();
            }
            return View(m);
        }
        [HttpPost]
        public ActionResult Edit(string title, string image, int rating, int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Movie m = db.Movies.Where(x => x.Id == id).FirstOrDefault();
                m.Title = title;
                m.Image = image;
                m.Rating = rating;
                db.SaveChanges();
                //db.Dispose(); using statement auto calls this function at the end
            }
            return RedirectToAction("Details/" + id);
        }
        public ActionResult Delete(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                //Post post = db.Posts.Where(x => x.Id == 3).FirstOrDefault();
                Movie m = db.Movies.Where(x => x.Id == id).FirstOrDefault();
                db.Movies.Remove(m);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}